/**
 * 
 */
package com.jm.web.builder;

import java.io.IOException;

import com.jm.utils.StringBuilderEx;
import com.jm.utils.StringHelper;
import com.jm.web.BasePage;
import com.jm.web.tags.BaseDPTag;
import com.jm.web.tags.TabTag;

/**
 * @author yanjie
 *
 * 2011/9/28 下午2:37:14
 */
public class TabBuilder extends BaseBuilder{
	protected TabTag tabTag = null;
	
	@Override
	public void RenderStart() {
		if(this.getBaseTag() instanceof TabTag)
			this.tabTag = (TabTag)this.getBaseTag();
		
		BaseDPTag parentTag =parentTag = (BaseDPTag)this.tabTag.getParent();
		if(this.getBaseTag().GetExtValue("keep", false)){
			StringBuilderEx sb = new StringBuilderEx();
			if(this.getBaseTag().GetExtValue("layout",true)){
				String id = this.tabTag.getId();
				if(StringHelper.IsNullOrEmpty(id)){
						id = StringHelper.Format("%1$s-%2$s",parentTag.getId(),parentTag.getCurrentTabIndex()+1);
				}
				
				sb.Append("\r\n<div id=\"%1$s\" style=\"height: auto;padding-top:2px;padding-right:0px;display:%2$s\">",
						id,
						parentTag.isHiddeTab(parentTag.getId()+"_tab"+parentTag.getCurrentTabIndex()+1) ? "no" : "");
			}
			
			try {
				this.getOut().write(sb.toString());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	@Override
	public void RenderEnd() {
		if(this.getBaseTag() instanceof TabTag)
			this.tabTag = (TabTag)this.getBaseTag();
		
		if(this.getBaseTag().GetExtValue("keep", false)){
			try {
				StringBuilderEx sb = new StringBuilderEx();
				
				if(this.getBaseTag().GetExtValue("layout",true)){
					sb.Append("</div>");
				}
				this.getOut().write(sb.toString());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else if(this.tabTag.getTab() != null){
			this.tabTag.getTab().onRender(this.getOut());
		}
		
		if(this.tabTag.getTab().getIframe() != null){
			StringBuilderEx sb = new StringBuilderEx();
			sb.Append("$P.iframe['ifr_%1$s'] = 'ifr_%1$s';",this.tabTag.getTab().getId());
			this.getBaseTag().getPage().RegisterOnReadyScript(BasePage.SCRIPTRUN_ONREADY,sb.toString());
		}
	}
}
